/**
 * @file:   Ancestor.Util.js
 * @path:   js/Ancestor/
 * @desc:
 * @author: karkarrotxu@qiyi.com
 * @date:   2017-08-15
 */
qui(function (require, exports, module) {


    ///import js-src/lib/
    ///import js-src/com/
    window.Ancestor = window.Ancestor || {
        version: '1.0'
    };
    Ancestor.Util = (function () {
        // 组装 sign
        var authorization = function (options) {
            var rawSign = options.url + options.clientTime + Ancestor.Config.EPG_AUTH.key;
            return qui.md5("hashString", rawSign);
        };

        var getUrlId = function (name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null)
                return r[2];
            return null;
        };

        /**
         * 限制输入框中字符个数，一个汉字相当于两个字符，wordCount是最多允许的汉字个数
         * @param  {jquery  object}  $des  限制字符个数的输入框
         * @param  {Number}  wordCount     允许输入的最多汉字个数,默认为450个汉字
         * @param  {Boolean} isFilterChars 是否过滤输入内容，true过滤，false或undefined不过滤
         * @param  {Array}  filterChars    需要过滤的字符集，如['$','.']，意为全局过滤 $ *                                 和 .
         */
        var limitWordCount = function ($des, wordCount, isFilterChars, filterChars) {
            wordCount = wordCount || 450;
            var t = function (textarea) {
                var $textarea = $(textarea);
                var text = $textarea.val();
                if (isFilterChars) {
                    // text = Com.string.filterSpecialChars(text, filterChars);
                    $textarea.val(text);
                }
                var length = Com.string.getStrLength(text);
                if (length > wordCount * 2) {
                    $textarea.val(Com.string.substrCn(text, wordCount));
                }
                text = $textarea.val();
                length = Com.string.getStrLength(text);
                $textarea.parent().find('span.leftWordsCount').text(wordCount - Math.round(length / 2));
            };
            $des.bind({
                input: function () {
                    t(this);
                },
                propertychange: function () {
                    t(this);
                },
                keyup: function () {
                    t(this);
                }
            });
            $des.keyup();
        };
        var getAuth = function (str) {
            var auth = {
                    'sign': '',
                    'clientTime': '',
                    'from': ''
                },
                time = parseInt(new Date().getTime() / 1000);
            auth.from = Ancestor.Config.EPG_AUTH.From;
            auth.clientTime = time;
            auth.sign = qui.md5("hashString", str + time + Ancestor.Config.EPG_AUTH.key);
            return auth;

        };

        var buildHeader = function(){
            var node1 = $('<nav class="navbar navbar-default navbar-fixed-top" role="navigation"></nav>');
            var node2 = $('<div class="container-fluid"></div>')
            var node2_1 = $('<div class="navbar-header"></div>');
            var node2_1_1 = $('<a class="navbar-brand" href="http://lego.iqiyi.com">LEGO</a>');
            var node2_2 = $('<div class="collapse navbar-collapse"></div>');
            node2_1_1.appendTo(node2_1);
            node2_1.appendTo(node2);
            node2_2.appendTo(node2);
            node2.appendTo(node1);
            node1.appendTo($("header.main-header"));
        }


        var buildMenu = function (usrData) {
            var parent = $("header div.navbar-collapse")
            var node5 = $('<ul class="nav navbar-nav j-lego-nav"></ul>');
            var node6 = $('<li class="dropdown "></li>');
            var node6_1 = $('<a href="javascript:;" class="dropdown-toggle">直播中心 <span class="caret"></span></a>');
            var node6_2 = $('<ul class="dropdown-menu dropdown-sub-nav" role="menu" data-cid="live"></ul>');
            var navListData = [];
            $.each(usrData, function (i, element) {
                navListData.push('<li class="navbar-sub-nav">');
                navListData.push(' <div class="navbar-sub-title"><span class="glyphicon glyphicon-th-large"></span>' + element.name + '</div>');
                navListData.push('<ul class="navbar-sub-list list-inline">');
                $.each(element.items, function (j, child) {
                    navListData.push('<li><a href="' + child.url + '" target="_blank">' + child.name + '</a></li>');
                })
                navListData.push('</ul>');
                navListData.push('</li>');
            })
            $(navListData.join("")).appendTo(node6_2);
            node6_1.appendTo(node6);
            node6_2.appendTo(node6);
            node6.appendTo(node5);
            node5.appendTo(parent);
        }
        var buildUserInfo = function (usrData) {
            var parent = $("header div.navbar-collapse")
            var htmlArr = [];
            htmlArr.push('  <ul class="nav navbar-nav navbar-right">');
            htmlArr.push('    <li class="dropdown" style="right: 30px">');
            htmlArr.push('         <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true"><span class="glyphicon glyphicon-user"></span>' + usrData.authInfo.userName + '</a>');
            htmlArr.push('        <ul class="dropdown-menu dropdown-user-info" role="menu"> ');
            htmlArr.push('            <li class="user-header"> ');
            htmlArr.push('               <p>' + usrData.authInfo.userName + '</p> ');
            htmlArr.push('                 <p><small>UID: ' + usrData.authInfo.uid + '</small></p> ');
            htmlArr.push('          </li>');
            htmlArr.push('           <li class="user-footer clearfix">');
            htmlArr.push('              <div class="text-center">');
            htmlArr.push(' <a href="'+usrData.loginOut+'" class="btn btn-default btn-block" data-ripple-color="#444">退出</a>');
            htmlArr.push('  </div></li></ul></li></ul>');
            parent.append(htmlArr.join(""));
        }

        var buildFooter = function (epg_version) {
            var htmlArr = [];
            htmlArr.push(' <footer class="main-footer">')
            htmlArr.push('   <div class="scroll-to-top scroll-to-top-hide" id="BackToTopButton">')
            htmlArr.push('     <a class="btn" href="javascript:;"><i class="glyphicon glyphicon-circle-arrow-up"></i>返回顶部</a>')
            htmlArr.push('  </div>')
            htmlArr.push('    <div class="container">')
            htmlArr.push('        <div class="footer-content">')
            htmlArr.push('           <p class="text-center">' + epg_version + ' Copyright © 2018 <a href="//www.iqiyi.com">iQIYI.COM</a> All rights reserved.</p>')
            htmlArr.push('           <p class="text-center">请使用 Chrome，Firefox，IE10+ 等浏览器访问本站</p>')
            htmlArr.push('       </div>')
            htmlArr.push('    </div>')
            htmlArr.push('  </footer>');
            $("body").append(htmlArr.join(""));
        }

        return {
            authorization: authorization,
            getUrlId: getUrlId,
            limitWordCount: limitWordCount,
            getAuth: getAuth,
            buildMenu: buildMenu,
            buildUserInfo: buildUserInfo,
            buildFooter: buildFooter,
            buildHeader:buildHeader
        };
    })();

});